<?php
namespace common\components;
use Bluerhinos\phpMQTT;

class Mqtt
{
    /**
     * 配置在/common/config/params
     * @param $topic
     * @param $content
     * @param $client_id
     * @return bool
     */
    public function sendMsg($topic,$content,$client_id){
        $mqtt = new phpMQTT(\Yii::$app->params['mqtt_server'], \Yii::$app->params['mqtt_port'], $client_id);
        if ($mqtt->connect(true, NULL, \Yii::$app->params['mqtt_username'],  \Yii::$app->params['mqtt_password'])) {
            $mqtt->publish($topic, $content, 0, false);
            $mqtt->close();
            return true;
        } else {
            return false;
        }
    }
}
